package ch.trainingtest.business;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ch.trainingtest.dao.UserDao;
import ch.trainingtest.dto.UserDto;
import ch.trainingtest.entity.User;

@Stateless
public class UserManagementBean {

	@EJB
	private UserDao userDao;
	
	public void createUser(String username, String password) {
		userDao.addUser(username, password);
	}
	
	public UserDto checkUserLogin(String username, String password) {
		User user = userDao.findUserByUsernameAndPassword(username, password);
		if(user!=null) {
			UserDto result = new UserDto();
			result.setId(user.getId());
			result.setUsername(user.getUsername());
//			result.setPassword(user.getPassword());
			return result;
		} else {
			return null;
		}
	}
	
	public List<UserDto> getAllUsers() {
		List<User> users = userDao.getAllUsers();
		List<UserDto> result = new ArrayList<UserDto>();
		for(User u : users) {
			result.add(new UserDto(u.getId(), u.getUsername(), u.getPassword()));
		}
		return result;
	}
}
